﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics; // for Process.Start

namespace EAPtoHTML
{
    class Program
    {
        private static string HelpMsg = @"

EAPtoHTML Help:

    EAPtoHTML is a very simple program. It does the following: 
    1. Open an existing EAP file
    2. Get data about all of the diagrams
    3. Create a random folder
    4. Save all of the Diagrams into that folder
    5. Save an index.html into that folder
    6. Open the random folder

    Upon completion you can:
    1. Browse the diagrams in the folder directly or...
    2. Text search in the index.html
        Note: You can click on the embedded links to the diagram images.

Syntax:

    EAPtoHTML [PATH_TO_EAP]

Examples:

    EAPtoHTML c:\somefolder\someeapfile.eap
    EAPtoHTML ""c:\some path or file\that has spaces\in it.eap""

Note:

    You will only see this message if:
    (!args.Length.Equals(1) || !File.Exists(args[0]) || !Path.GetExtension(args[0]).ToUpper().Equals(""EAP""))
    
    Which means:
    1. Your argument count is not 1
    2. Your file doesn't exist
    3. Your file isn't an EAP file

";
        static void Main(string[] args)
        {
            //sanity checks on args
            if (!args.Length.Equals(1) || !File.Exists(args[0]) || !Path.GetExtension(args[0]).ToUpper().Equals(".EAP"))
            {
                Console.WriteLine(HelpMsg);
            }
            else
            {
                /*try
                {*/
                    //status message start
                    DateTime start = DateTime.Now;
                    Console.WriteLine("Started!");

                    // get the path of the file we are getting data from
                    string WhereIsTheEAPFile = args[0];

                    // create some temp folder to dump the data to, you could use any method here of course
                    string WhereDoYouWantToSaveTheFiles = Guid.NewGuid().ToString().Insert(0, Path.GetTempPath());
                    while (Directory.Exists(WhereDoYouWantToSaveTheFiles)) //make sure it doesn't already exist
                        WhereDoYouWantToSaveTheFiles = Guid.NewGuid().ToString().Insert(0, Path.GetTempPath());
                    Directory.CreateDirectory(WhereDoYouWantToSaveTheFiles);//create the dir

                    Action DoIt = () =>
                    {
                        //some timing for testing
                        DateTime timer = DateTime.Now;

                        //setup
                        Console.Write("setting up EA... ");
                        EA.Repository r = new EA.Repository();
                        Console.WriteLine("done! ({0} elapsed)", (DateTime.Now - timer).ToString());
                        timer = DateTime.Now;

                        //open the EAP file
                        Console.Write("opening EAP file {0}... ", WhereIsTheEAPFile);
                        r.OpenFile(WhereIsTheEAPFile);
                        Console.WriteLine("done! ({0} elapsed)", (DateTime.Now - timer).ToString());
                        timer = DateTime.Now;

                        //export the data to a static html site
                        r.toHTML(WhereDoYouWantToSaveTheFiles);

                        //close the file
                        Console.Write("closing EAP file... ");
                        r.CloseFile();
                        Console.WriteLine("done! ({0} elapsed)", (DateTime.Now - timer).ToString());
                        timer = DateTime.Now;

                        //cleanup
                        Console.Write("cleaning up... ");
                        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(r); //perhaps this is a bit dangerous?
                        r = null;
                        Console.WriteLine("done! ({0} elapsed)", (DateTime.Now - timer).ToString());
                        timer = DateTime.Now;

                        //show the folder
                        Console.Write("opening export folder... ");
                        Process.Start(WhereDoYouWantToSaveTheFiles);
                        Console.WriteLine("done! ({0} elapsed)", (DateTime.Now - timer).ToString());
                        timer = DateTime.Now;
                    };

                    DoIt();
                    Console.WriteLine("Finished!\nElapsed time was {0}", (DateTime.Now - start).ToString());
                /*}
                catch (Exception ex)
                {
                    Console.WriteLine("\n\n\tUh oh....\tSomething bad happened...\n\n\tSorry... I don't really 'handle' any errors. \n\n\tError message and stack trace to follow! =)\n\n\n");
                    Console.WriteLine("Error Message:\n\n{0}\n\n",ex.Message);
                    Console.WriteLine("Error StackTrace:\n\n{0}\n\n",ex.StackTrace);
                }*/
                Console.ReadKey();
            }
        }
    }
}
